<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller;

use app\common\controller\Admin;
use app\rehearse\controller\api\Plan as PlanApi;
use app\rehearse\controller\api\Script as ScriptApi;
use app\admin\controller\ajax\Library as Ajax;


/**
 * 脚本管理
 * @package app\plan
 */
class Script  extends Admin

{
    
    protected $api;
    
	public function _initialize()
	{
		 parent::_initialize();
		 $this->script = new ScriptApi();
              $this->ajax= new Ajax();
              $this->api = new PlanApi();
            
	}
    /**
     * 首页
     * 
     */
   	public function index() {
       
   	  if(IS_AJAX){
   	  	$planList = $this->api->planList();
   	  	return $planList;
   	  }

      $unitList = model('basic/unit')->column('fullName','id');
      $unitList[0]='管委会';
      $this->assign('unitList',$unitList);
      $type=$this->ajax->getplanType()->getData();
      $this->assign('type',$type);
      return $this->fetch();
 	}

 	/**
 	 * 脚本搜索
 	 */
 	public function search($typeId,$unitId) {
           
 		if(IS_AJAX){
                  
			$planList = $this->api->search(input('typeId'),input('unitId'));
 			return $planList;
 		}
 	}
    /**
     * 添加脚本
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.');  
   		 	//有效性
                   if(isset($data['isValid'])){
                     $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
                     $data['applyTime']=time();
                  return $this->api->add($data);
   		 }
           $unitList = model('basic/unit')->column('fullName','id');
           $unitList[0]='管委会';
           $this->assign('unitList',$unitList);

           $type=$this->ajax->getplanType()->getData();
         
           $this->assign('type',$type);
          
           return $this->fetch();    
     }
 
    /**
     * 修改脚本
     * 
     */
    public function edit($planId = null) {
      
    	  if(IS_POST){
    	  	$data = input('post.');
                     if(isset($data['isValid'])){
                     $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
    	  	return $this->api->edit($data,$planId);
    	  }
           $unitList = model('basic/unit')->column('fullName','id');
           $unitList[0]='管委会';
           $this->assign('unitList',$unitList);

           $planList = $this->api->planList($planId)->getData();
           $type=$this->ajax->getplanType()->getData();
         
           $this->assign('type',$type);
           $this->assign('info',$planList['data'][0]);
           return $this->fetch();    
    }
    
    /**
     * 删除脚本
     */
    public function del($planId = null) {
    	if(IS_AJAX){
    		return $this->api->del($planId);
    	}
    }


     /**
     * 脚本步骤列表
     */
    public function scriptList($planId = null) {
        if(IS_AJAX){
        $scriptList = $this->script->scriptList($planId);
         return $scriptList;
      }
      $unitList = model('basic/unit')->column('fullName','id');
      $unitList[0]='管委会';
      $this->assign('unitList',$unitList);
      $this->assign('planId',$planId);
      return $this->fetch();
     }

      /**
     * 脚本步骤列表
     */
    public function scriptAdd($planId = null) {

      if(empty($planId)){
        return 0;
      }
      if(IS_POST){
              $data = input('post.');  
              $data['reservePlanId']=$planId;
              return $this->script->add($data);
            }
      $unitList = model('basic/unit')->column('fullName','id');
      $unitList[0]='管委会';
      $this->assign('unitList',$unitList);
	  $unitId = model('Plan')->where('id',$planId)->value('unitId');
	  $this->assign('unitId',$unitId);
      $type = $this->ajax->getScriptType()->getData();
      $this->assign('planId',$planId);
      
      $this->assign('type',$type);
     return $this->fetch();       
     }


    /**
     * 修改脚本步骤
     * 
     */
    public function scriptedit($planId = null,$scriptId = null) {
      
        if(IS_POST){
          $data = input('post.');
          $data['reservePlanId']=$planId;
          return $this->script->edit($data,$scriptId);
        }

        $scriptList = $this->script->scriptList($planId,$scriptId)->getData();

        $unitList = model('basic/unit')->column('fullName','id');
        $unitList[0]='管委会';
        $this->assign('unitList',$unitList);

        $type=$this->ajax->getScriptType()->getData();

         $this->assign('type',$type);
         $this->assign('info',$scriptList['data'][0]);
         $this->assign('planId',$planId);
         return $this->fetch();    
    }




    /**
     * 删除脚本步骤
     */
    public function scriptdel($scriptId = null) {
      if(IS_AJAX){

        return $this->script->del($scriptId);
      }
    }

    /**
     * 申请审核
     * @param string $id
     */
    public function audit($id = null){
    	return $this->api->audit($id);
    }


    }



